package org.spring.boot.rabbitmq.send;

import org.spring.boot.rabbitmq.conf.RabbitExchangeConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: eastcom
 * @desc: 发送消息
 **/
@Service
public class SendMessage {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private RabbitExchangeConfig exchangeConfig;

	/**
	 * 发送一对一消息
	 * @param object 消息
	 */
	public void sendDirectMessage(Object object){
		rabbitTemplate.convertAndSend(exchangeConfig.getCacheDirect(), object);
	}

	/**
	 * 通过关键KEY字段过滤进行一对多发送
	 * @param key
	 * @param message
	 */
	public void sendTopicMessage(String key, Object message){
		rabbitTemplate.convertAndSend(exchangeConfig.getCacheTopic(), key, message);
	}

	/**
	 * 广播模式发送
	 * @param message
	 */
	public void sendFanoutMessage(Object message){
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		rabbitTemplate.setExchange(exchangeConfig.getCacheFanout());
		rabbitTemplate.setRoutingKey("*");

		rabbitTemplate.convertAndSend(message);
	}
}
